﻿using System;
using System.Collections.Generic;
using System.Linq;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.Core.BLL
{
    public class FeedbackListView
    {
        public int ContactId { get; set; }
        public string ContactName { get; set; }
        public string ContactEmail { get; set; }
        public string ContactTitle { get; set; }
        public string ContactBody { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public string Title { get; set; }
        public DateTime CreateDate { get; set; }
    }

    public class Feedback
    {
        public static Feedback Instance
        {
            get { return new Feedback(); }
        }

        public static FeedbackListView LoadDataFromListItem(Contact item)
        {
            var objFeedBack = new FeedbackListView
                                  {
                                      Address = item.Address,
                                      ContactBody = item.Body,
                                      ContactEmail = item.ContactEmail,
                                      ContactId = item.ContactId,
                                      ContactName = item.ContactName,
                                      ContactTitle = item.Title,
                                      Fax = item.Fax,
                                      Phone = item.Phone,
                                      Title = item.Title,
                                      CreateDate = Convert.ToDateTime(item.CreateDate)
                                  };
            return objFeedBack;
        }

        /// <summary>
        /// Create Contact
        /// </summary>
        /// <param name="address">Address</param>
        /// <param name="body">Body</param>
        /// <param name="email">Email Contact</param>
        /// <param name="contactName">Name Contact</param>
        /// <param name="title">Title Contact</param>
        /// <param name="fax">Fax</param>
        /// <param name="phone">Phone</param>
        /// <returns></returns>
        public bool Create(string address, string body, string email, string contactName, string title, string fax, string phone)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFeedback = new Contact
                                          {
                                              Address = Common.Instance.Truncate(address, 150, false, false),
                                              Body = Common.Instance.Truncate(body, 4000, false, false),
                                              ContactEmail = Common.Instance.Truncate(email, 50, false, false),
                                              ContactName = Common.Instance.Truncate(contactName, 30, false, false),
                                              Title = Common.Instance.Truncate(title, 150, false, false),
                                              Fax = Common.Instance.Truncate(fax, 30, false, false),
                                              Phone = Common.Instance.Truncate(phone, 30, false, false),
                                              CreateDate = DateTime.Now
                                          };

                    dc.Contacts.InsertOnSubmit(objFeedback);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Contact
        /// </summary>
        /// <param name="address">Address</param>
        /// <param name="body">Body</param>
        /// <param name="email">Email</param>
        /// <param name="contactName">Contact Name</param>
        /// <param name="title">Contact Title</param>
        /// <param name="fax">Fax</param>
        /// <param name="phone">Phone</param>
        /// <param name="contactId">Contact Id</param>
        /// <returns></returns>
        public bool Update(string address, string body, string email, string contactName, string title, string fax, string phone, int contactId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFeedback = (dc.Contacts.Where(p => p.ContactId == contactId)).Single();
                    objFeedback.Address = Common.Instance.Truncate(address, 150, false, false);
                    objFeedback.Body = Common.Instance.Truncate(body, 4000, false, false);
                    objFeedback.ContactEmail = Common.Instance.Truncate(email, 50, false, false);
                    objFeedback.ContactName = Common.Instance.Truncate(contactName, 30, false, false);
                    objFeedback.Title = Common.Instance.Truncate(title, 150, false, false);
                    objFeedback.Fax = Common.Instance.Truncate(fax, 30, false, false);
                    objFeedback.Phone = Common.Instance.Truncate(phone, 30, false, false);

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Contact
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns></returns>
        public bool Delete(int contactId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFeedback = (dc.Contacts.Where(p => p.ContactId == contactId)).Single();

                    dc.Contacts.DeleteOnSubmit(objFeedback);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get Contact By Id
        /// </summary>
        /// <param name="contactId">Contact Id</param>
        /// <returns></returns>
        public FeedbackListView GetById(int contactId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from q in dc.Contacts
                            where q.ContactId == contactId
                                select q;

                return LoadDataFromListItem(query.SingleOrDefault());
            }
        }

        /// <summary>
        /// Get All Contact
        /// </summary>
        /// <returns></returns>
        public List<FeedbackListView> GetAll()
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from q in dc.Contacts
                            orderby q.CreateDate descending
                            select new FeedbackListView
                            {
                                Address = q.Address,
                                ContactBody = q.Body,
                                ContactEmail = q.ContactEmail,
                                ContactId = q.ContactId,
                                ContactName = q.ContactName,
                                ContactTitle = q.Title,
                                Fax = q.Fax,
                                Phone = q.Phone,
                                Title = q.Title,
                                CreateDate = Convert.ToDateTime(q.CreateDate)
                            };

                return query.ToList();
            }            
        }
    }
}
